package com.project.market.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.project.market.db.domain.MarketKeywordExample;
import com.project.market.db.domain.MarketUser;
import com.project.market.db.domain.MarketUserExample;
import com.project.market.db.mapper.MarketUserMapper;
import com.project.market.service.AdminUserService;
import com.project.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminUserServiceImpl implements AdminUserService {
    @Override
    public List<MarketUser> list(String page, String limit, String sort, String order,String username,String mobile) {

        SqlSession session = MybatisUtil.getSession();
        MarketUserMapper mapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if(username!=null){
            criteria.andUsernameLike("%" + username +"%");
        }
        if(mobile!=null && !("".equals(mobile))){
            criteria.andMobileEqualTo(mobile);
        }

        List<MarketUser> list = mapper.selectByExampleSelective(example);

        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));

        session.commit();
        session.close();

        return list;
    }

    @Override
    public MarketUser detail(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketUserMapper mapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if(id!=null && !("".equals(id))){
            try {
                criteria.andIdEqualTo(Integer.valueOf(id));
            }
            catch (Exception e){
                return null;
            }
        }

        MarketUser marketUser = mapper.selectOneByExample(example);

        PageHelper.startPage(1,20);

        session.commit();
        session.close();

        return marketUser;
    }
}
